package com.atos.howl.chaos.web.controller.tougao;

import com.atos.howl.chaos.enums.ChaosSysCodeEnum;
import com.atos.howl.chaos.pdm.dto.VideoAppendInfo;
import com.atos.howl.chaos.pdm.entity.ChaosVideo;
import com.atos.howl.chaos.pdm.entity.ChaosVideoCoverImg;
import com.atos.howl.chaos.pdm.entity.ChaosVideoFenqu;
import com.atos.howl.chaos.pdm.service.VideoService;
import com.atos.howl.chaos.utils.ChaosUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.Date;


@Controller
@RequestMapping("/api/tougaoController")
public class TougaoController {

    @Autowired
    private VideoService videoService;

    @PostMapping("/tougao")
    @ResponseBody
    @Transactional
    public ChaosSysCodeEnum tougao(@RequestParam("videoFile") MultipartFile videoFile,//视频文件
                                   @RequestParam("videoCoverImg220x125") String videoCoverImg220x125,//视频预览图1
                                   @RequestParam("videoCoverImg325x246") String videoCoverImg325x246,//视频预览图1
                                   @RequestParam("videoCoverImg470x200") String videoCoverImg470x200,//视频预览图1
                                   @RequestParam("videoCoverImg80x80") String videoCoverImg80x80,//视频预览图1
                                   @RequestParam("videoFenquParent") String videoFenquParent,//视频分区父字段
                                   @RequestParam("videoFenquChild") String videoFenquChild,//视频分区子字段
                                   @RequestParam("videoTitle") String videoTitle,//视频标题
                                   @RequestParam("videoSummary") String videoSummary//视频简介
                               ) throws IOException {

        long currentTime = new Date().getTime();
        String videoName = currentTime+"_"+videoFile.getOriginalFilename();
        String videoPath = "http://localhost:8082/chaos/video/"+videoName;
        String videoFilePath = "D:/chaosVideoServer/"+videoName;

        ChaosVideo chaosVideo = new ChaosVideo();
        chaosVideo.setVideoName(videoName);
        chaosVideo.setVideoPath(videoPath);
        String userUUID = ChaosUtils.getLoginUserUUID();
        chaosVideo.setUserUuid(userUUID);
        chaosVideo.setVideoTitle(videoTitle);
        chaosVideo.setVideoSummary(videoSummary);

        videoService.saveVideoInfo(chaosVideo);
        int videoId = chaosVideo.getVideoId();
        //通过CommonsMultipartFile的方法直接写文件（注意这个时候）
        File newFile=new File(videoFilePath);
        videoFile.transferTo(newFile);

        ChaosVideoCoverImg coverImg = new ChaosVideoCoverImg();
        coverImg.setVideoId(videoId);
        coverImg.setVideoCoverImg220x125(videoCoverImg220x125);
        coverImg.setVideoCoverImg325x246(videoCoverImg325x246);
        coverImg.setVideoCoverImg470x200(videoCoverImg470x200);
        coverImg.setVideoCoverImg80x80(videoCoverImg80x80);
        coverImg.setUserUuid(ChaosUtils.getLoginUserUUID());
        //保存封面图信息
        videoService.saveChaosVideoCoverImg(coverImg);


        ChaosVideoFenqu fenqu = new ChaosVideoFenqu();
        fenqu.setVideoId(videoId);
        fenqu.setFenquParent(videoFenquParent);
        fenqu.setFenquChild(videoFenquChild);
        //保存分区信息
        videoService.saveVideoFenqu(fenqu);

        //保存附加信息逻辑
        VideoAppendInfo videoAppendInfo = new VideoAppendInfo();
        videoAppendInfo.setVideoId(videoId);
        videoAppendInfo.setVideoBofang(0);
        videoAppendInfo.setVideoDianzan(0);
        videoAppendInfo.setVideoToubi(0);
        videoService.saveVideoAppendInfo(videoAppendInfo);

        return ChaosSysCodeEnum.TOUGAO_SUCCESS;
    }
}
